﻿using Microsoft.IdentityModel.Tokens;
using System;
using System.Threading.Tasks;

namespace CommonDevelop.Operator
{
    /// <summary>
    /// JWT发行人选项(打包appsettings.json的配置)
    /// </summary>
    [Serializable]
    public class JwtOptions
    {
        /// <summary>
        /// 发行人信息
        /// </summary>
        public string Issuer { get; set; }

        /// <summary>
        /// 主题信息
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// 接收者信息
        /// </summary>
        public string Audience { get; set; }

        /// <summary>
        /// 发布时间
        /// </summary>
        public DateTime IssuedAt => DateTime.UtcNow;

        /// <summary>
        /// 设置令牌有效的时间跨度（默认值为7200秒）
        /// </summary>
        public double ValidFor { get; set; }

        /// <summary>
        /// 截止日期
        /// </summary>
        public DateTime Expiration => IssuedAt.AddSeconds(ValidFor);

        /// <summary>
        /// 不早于索赔确定，不接受JWT进行处理的时间
        /// </summary>
        public DateTime NotBefore => DateTime.UtcNow;

        /// <summary>
        /// "jti" (JWT ID) Claim (default ID is a GUID)
        /// </summary>
        public Func<Task<string>> JtiGenerator => async () => await Task.FromResult(Guid.NewGuid().ToString("N"));

        /// <summary>
        /// The signing key to use when generating tokens.
        /// </summary>
        public SigningCredentials SigningCredentials { get; set; }
    }
}
